/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TAREAlmacenamientoGESTORtupla.java
 *
 * Created on 22-jun-2012, 20:55:18
 */
package Almacenamiento;

import java.awt.Component;
import java.awt.event.KeyEvent;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.DefaultCellEditor;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.SwingUtilities;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author User
 */
public class TAREAlmacenamientoGESTORtupla extends javax.swing.JFrame {
    
    List<Relacion> Lista_Relaciones ;
    Relacion Relacion_actual;
    
    public TAREAlmacenamientoGESTORtupla(){
    Lista_Relaciones= new ArrayList<Relacion>();    
        
    initComponents();
   String[] DATA = { "String", "Float", "Double", "Char", "Int", "Byte", "Short" ,"Long" };
    JComboBox comboBox = new JComboBox(DATA);
    comboBox.setSelectedIndex(0);
    DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
    jTable1.getColumnModel().getColumn(1).setCellEditor(defaultCellEditor);
            
    }
  
    /** Creates new form TAREAlmacenamientoGESTORtupla */
   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setIcon(new javax.swing.ImageIcon("D:\\ico java\\run.PNG")); // NOI18N
        jButton1.setText("Tarea 1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon("D:\\ico java\\run.PNG")); // NOI18N
        jButton2.setText("Crear");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Atributos", "Tipo"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        jLabel1.setText("Nueva Relacion:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Tarea 2");

        jLabel3.setText("Mostrar Relacion");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton3.setIcon(new javax.swing.ImageIcon("D:\\ico java\\run.PNG")); // NOI18N
        jButton3.setText("Mostrar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 148, Short.MAX_VALUE)
        );

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTable2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                CLickTable(evt);
            }
        });
        jScrollPane3.setViewportView(jTable2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 854, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 852, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(310, 310, 310))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 27, Short.MAX_VALUE)
                                .addComponent(jButton3)))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 422, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(78, 78, 78))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    /*1) Representar el esquema de la relación 
    Esquema_Cliente = (id_cliente, nombre_cliente, direccion_cliente, telefono_cliente), donde:
    id_cliente tiene dominio de valores enteros,
    nombre_cliente tiene dominio de valores de cadena de caracteres,
    direccion_cliente tiene dominio de valores de cadena de caracteres,
    telefono_cliente tiene dominio de valores entero largo.
    Seguidamente, muestre el esquema creado en pantalla. Modifique el esquema de tal manera 
     * que se añada un nuevo atributo denominado tipo_cliente con dominio de valores de tipo 
     * carácter luego muéstrelo en pantalla. Finalmente modifique nuevamente el esquema para 
     * eliminar el atributo telefono_cliente y luego muestre el esquema resultante.*/
    //ejecutar sentencia 
    //verificar la correctitud  -> falñta implementar asumir k la sintaxis es correcta y k se está creando un tabla

    Relacion Cliente = new Relacion();
    List<Atributo> at = new ArrayList<Atributo>();
    at.add(new Atributo("id_Cliente", Integer.class));
    at.add(new Atributo("nombre_cliente", String.class));
    at.add(new Atributo("direccion_cliente", String.class));
    at.add(new Atributo("telefono_Cliente", Long.class));

    Cliente.setAtributos(at);
    jTextArea1.setText("1." + Cliente.toString());
    at.add(new Atributo("tipo_cliente", Character.class));
    Cliente.setAtributos(at);
    jTextArea1.setText(jTextArea1.getText() + " \n 2." + Cliente.toString());
    Atributo att = new Atributo("telefono_Cliente", Long.class);
    at.remove(att);
    Cliente.setAtributos(at);
    jTextArea1.setText(jTextArea1.getText() + " \n 3." + Cliente.toString());
    
 
   






}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    /*
    2) Utilizando las Clases de Software implementado en la Gestión de Tuplas, 
     * desarrolle una aplicación para realizar cualquier Esquema de Relación, donde se pueda 
     * ingresar "n" tuplas las cuales deben ser escritas en una array de bytes y 
     * recuperadas del mismo array.
     * 
     * 
     */
    //crear la relacion
    Relacion R= new Relacion();
    List<Atributo> att= new ArrayList<Atributo>();
    int nro = jTable1.getRowCount();
    for (int i = 0; i < nro; i++) {
        String clase= jTable1.getValueAt(i, 1).toString();
        if ("String".equals(clase)) {
            
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),String.class));
        }
        if ("Int".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Integer.class));
        }
        if ("Long".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Long.class));
        }
        if ("Char".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Character.class));
        }
         if ("Double".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Double.class));
        }
          if ("Float".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Float.class));
        }
           if ("Shor".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Short.class));
        }
         if ("Byte".equals(clase)) {
            att.add(new Atributo(jTable1.getModel().getValueAt(i, 0).toString(),Byte.class));
        }
       
                
    }
    
    R.setAtributos(att);
    
    //JTable tabla_r= new JTable();
    //agregando dinamicamente tablas
    try{
        DefaultTableModel modelo=(DefaultTableModel) jTable2.getModel();

    for (int i = 0; i < att.size(); i++) {
        
        modelo.addColumn(att.get(i));
    }
    
    modelo.addRow(new Object[] {"",""});
     Lista_Relaciones.add(R);
     Relacion_actual=R;
    JOptionPane.showMessageDialog(this,"La relación se agregó correctamente.");
    }
    catch(Exception ee)
    {
    JOptionPane.showMessageDialog(this,ee.getMessage());
    }
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jButton3ActionPerformed

private void CLickTable(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CLickTable
//
   
    if (evt.getKeyChar()== KeyEvent.VK_ENTER) {//si se presiona enter
        
        int  fila = jTable2.getSelectedRow();
       //insertar la nueva tupla 
        
        
        GestorIOTupla gestor = new GestorIOTupla(Relacion_actual, "Relaciones");
        Tupla tupla= new Tupla();
        tupla.setValores(null);

        
        
    }
    
    
    
}//GEN-LAST:event_CLickTable

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TAREAlmacenamientoGESTORtupla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TAREAlmacenamientoGESTORtupla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TAREAlmacenamientoGESTORtupla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TAREAlmacenamientoGESTORtupla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TAREAlmacenamientoGESTORtupla().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    private boolean EsCorrecto(String text) {

        //falta implementar
        return true;


    }
   
}
